package com.yang.code_sample.performance_count.controller;

import com.yang.code_sample.performance_count.entity.RequestInfo;
import com.yang.code_sample.performance_count.service.MetricsStorage;
import org.springframework.util.StringUtils;

/**
 * @Description: 统计入口控制层
 * @Author: 杨 强
 * @CreateTime: 2022-10-13 16:44
 */
public class MetricsCollector {

    // 统计数据的接口(基于接口编程而非实现)
    private MetricsStorage metricsStorage;

    // 构造注入(依赖注入,而不是直接New)
    public MetricsCollector(MetricsStorage metricsStorage) {
        this.metricsStorage = metricsStorage;
    }

    /**
     * 记录请求数据方法
     * @param requestInfo 请求数据
     */
    public void recordRequest(RequestInfo requestInfo){
        if (requestInfo == null || StringUtils.isEmpty(requestInfo.getApiName())){
            return;
        }
        metricsStorage.saveRequestInfo(requestInfo);
    }
}
